package operation.modules.operation.controller;

import operation.modules.operation.entity.StatisticEntity;
import operation.modules.operation.service.StatisticService;
import operation.util.PageUtil;
import operation.util.ReqResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("api/operation")
public class StatisticController {
    @Autowired
    private StatisticService statisticService;
    @Resource
    protected HttpServletResponse response;

    /**
     * 工时统计列表
     */
    @RequestMapping("/list")
    public ReqResult list(@RequestParam Map<String, Object> params) {
        PageUtil page = statisticService.queryPage(params);
        return ReqResult.ok().put("page", page);
    }

    /**
     * 导入数据
     */
    @RequestMapping("/import")
    public ReqResult importData(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {
        return statisticService.importData(file, type);
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    public ReqResult export(@RequestBody Map<String, Object> params) {
        statisticService.export(response, params);
        return ReqResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ReqResult delete(@RequestBody Long[] ids) {
        statisticService.deleteByIds(Arrays.asList(ids));
        return ReqResult.ok();
    }

    /**
     * 根据id获取信息
     */
    @RequestMapping("/info/{id}")
    public ReqResult openInfo(@PathVariable("id") int id) {
        StatisticEntity statistic = statisticService.getById(id);
        return ReqResult.ok().put("statistic", statistic);
    }

    /**
     * 修改信息
     */
    @RequestMapping("/update")
    public ReqResult update(@RequestBody StatisticEntity statistic) {
        statistic.setUpdateTime(new Date());
        statisticService.updateStatistic(statistic);
        return ReqResult.ok();
    }
}
